<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:s="http://jboss.com/products/seam/taglib"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j"
    template="/layout/template.xhtml">

<ui:define name="body">

    <h:form id="productListForm">

        <rich:panel>
            <f:facet name="header">ProductList</f:facet>

            <s:decorate id="nameField" template="/layout/edit.xhtml">
                <ui:define name="label">Name</ui:define>
                <h:inputText id="name" required="true"
                             value="#{productListHome.instance.name}"/>
            </s:decorate>
            
            <div style="clear:both"/>
            
            <h:form id="productListEntriesForm">
                <s:validateAll />
                <rich:dataTable id="productListEntriesList" var="entry" value="#{listEntries}">
                    <f:facet name="header">ProductListEntries</f:facet>
                    <rich:column styleClass="#{not empty facesMessages.getCurrentMessagesForControl('product') ? 'errors' : ''}">
                        <f:facet name="header">Name</f:facet>
                        <h:outputText value="#{entry.product.name}" rendered="#{toEditProductListEntry != entry}" />
                        <h:selectOneMenu id="product" value="#{entry.product}" required="true" rendered="#{toEditProductListEntry == entry}">
                            <s:selectItems noSelectionLabel="- Select a Product -" value="#{baseProductList.resultList}" var="product" label="#{product.name}" />
                            <s:convertEntity />
                        </h:selectOneMenu>
                        <rich:toolTip for="product" rendered="#{not empty facesMessages.getCurrentMessagesForControl('product')}">
                            <h:graphicImage value="/image/error.gif" styleClass="errors" />
                            <h:message for="product" />
                        </rich:toolTip>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">Pieces</f:facet>
                        <h:outputText value="#{entry.count}" rendered="#{toEditProductListEntry != entry}" />
                        <h:inputText value="#{entry.count}" rendered="#{toEditProductListEntry == entry}" />
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">Actions</f:facet>
                        <h:panelGroup rendered="#{toEditProductListEntry != entry}" >
                            <a4j:commandLink id="edit" value="Edit" action="#{productListHome.editProductListEntry}" reRender="productListEntriesList" ajaxSingle="true" />
                            #{' '}
                            <a4j:commandLink id="delete" value="Delete" action="#{productListHome.deleteProductListEntry}" reRender="productListEntriesList" ajaxSingle="true" />
                        </h:panelGroup>
                        <h:panelGroup rendered="#{toEditProductListEntry == entry}" >
                            <a4j:commandLink id="save" value="Save" action="#{productListHome.saveEditProductListEntry}" reRender="productListEntriesList" />
                            #{' '}
                            <a4j:commandLink id="cancel" value="Cancel" action="#{productListHome.cancelEditProductListEntry}" reRender="productListEntriesList" ajaxSingle="true" />
                        </h:panelGroup>
                    </rich:column>
                    <f:facet name="footer">
                        <h:panelGroup>
                            <a4j:commandButton id="addProductListEntry" value="Add ProductListEntry" action="#{productListHome.addProductListEntry}" reRender="productListEntriesList" ajaxSingle="true" />
                            <h:commandButton  id="showAddRecipe" value="Add Ingredients for Recipe" onclick="#{rich:component('selectRecipeDialog')}.show(); return false;"/>
                        </h:panelGroup>
                    </f:facet>
                </rich:dataTable>
            </h:form>
            
            <div style="clear:both"/>
        </rich:panel>

        <div class="actionButtons">
            <h:commandButton id="save"
                          value="Save"
                         action="#{productListHome.persist}"
                       rendered="#{!productListHome.managed}"/>
            <h:commandButton id="update"
                          value="Save"
                         action="#{productListHome.update}"
                       rendered="#{productListHome.managed}"/>
            <h:commandButton id="delete"
                          value="Delete"
                         action="#{productListHome.remove}"
                      immediate="true"
                       rendered="#{productListHome.managed}"/>
            <s:button propagation="end"
                               id="cancel"
                            value="Cancel"
                             view="/view/productListList.xhtml"/>
        </div>

    </h:form>
            
            <rich:modalPanel id="selectRecipeDialog" moveable="false">
                <f:facet name="header">
                    <h:outputText value="Select a Recipe" />
                </f:facet>
                <h:form id="recipeSelectionForm">
                <s:validateAll />
                <h:panelGrid columns="1">
                    <h:panelGroup layout="block">
                        <h:selectOneMenu id="recipe" value="#{productListHome.toAddRecipe}" required="false">
                            <s:selectItems value="#{productListHome.recipes}" var="recipe" label="#{recipe.name}" />
                            <s:convertEntity />
                        </h:selectOneMenu>
                    </h:panelGroup>
                    <h:panelGroup layout="block">
                        <a4j:commandButton id="addRecipe" value="Add Ingredients for Recipe" action="#{productListHome.addProductListEntriesForRecipe}" reRender="productListEntriesList" oncomplete="#{rich:component('selectRecipeDialog')}.hide();" />
                        <h:commandButton id="hideAddRecipe" value="Cancel" onclick="#{rich:component('selectRecipeDialog')}.hide(); return false;"/>
                    </h:panelGroup>
                </h:panelGrid>
                </h:form>
            </rich:modalPanel>

</ui:define>

</ui:composition>
